package com.csx.micrometer.demo.consumer.controller;

import com.csx.micrometer.demo.consumer.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author cuisongxu
 * @date 2022/11/15 19:04
 */
@RestController
@RequestMapping
public class MicrometerThreadPoolController {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @GetMapping("/test")
    public String test() throws InterruptedException {
        while(true) {
            Thread.sleep(100);
            threadPoolExecutor.execute(() -> {
                try {
                    Thread.sleep(RandomUtil.nextInt(100,2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("hahah");
            });
        }

    }

    @RequestMapping("/testMethod")
    public String testMethod() {
        return "testMethod";
    }

    @RequestMapping("/testMethodBody")
    public String testMethodBody(@RequestBody Map<String, String> map) {
        String value = map.get("key");
        return value;
    }

    @GetMapping("/test2")
    public String test2(@RequestBody Map<String,String> map) {
        return "Sadsad";
    }
}
